#include "cppTutorDynamicCast.h"

#include <iostream>

using namespace std;

CcppTutorDynamicCast :: CcppTutorDynamicCast () {
}

CcppTutorDynamicCast :: ~CcppTutorDynamicCast () {
}

void CcppTutorDynamicCast :: m_DynamicCast () {
    cout << "ENTER : CcppTutorDynamicCast :: m_DynamicCast" << endl;
    CcppTutorDynamicCast* dc = new CcppTutorDynamicCast;
    vpPtr = dynamic_cast <void*> (dc);
    if (dc == NULL) {
        cout << "dynamic_Cast fails" << endl;
    } 
    else {
        dc->print ();
    }
    delete dc;
    cout << "EXIT  : CcppTutorDynamicCast :: m_DynamicCast" << endl;
}

void CcppTutorDynamicCast :: print () {
    cout << "-------------------------" << endl;
    cout << "--        SUCCESS      --" << endl;
    cout << "-------------------------" << endl;
}

CcppTutorDynamicCastAnother :: CcppTutorDynamicCastAnother () {
}

CcppTutorDynamicCastAnother :: ~CcppTutorDynamicCastAnother () {
}

void CcppTutorDynamicCastAnother :: m_DynamicCast () {
    cout << "ENTER : CcppTutorDynamicCastAnother :: m_DynamicCast" << endl;
    CcppTutorDynamicCast* dc = new CcppTutorDynamicCast;
    vpPtr = dynamic_cast <void*> (dc);
    if (dc == NULL) {
        cout << "dynamic_Cast fails" << endl;
    } 
    else {
        dc->print ();
    }
    delete dc;
    cout << "EXIT  : CcppTutorDynamicCastAnother :: m_DynamicCast" << endl;
}

void someFunc (CcppTutorDynamicCast* dc, CcppTutorDynamicCastAnother* dca) {
    cout << "ENTER : someFunc" << endl;
    
    // this will call the derived class version
    CcppTutorDynamicCast* dc1 = dynamic_cast <CcppTutorDynamicCast*> (dca);
    if (dc1 == NULL) {
        cout << "dynamic_Cast fails" << endl;
    } 
    else {
        dc1->m_DynamicCast ();
    }
    
    // this will call the base class version
    CcppTutorDynamicCast* dc2 = dynamic_cast <CcppTutorDynamicCast*> (dc);
    if (dc2 == NULL) {
        cout << "dynamic_Cast fails" << endl;
    } 
    else {
        dc2->m_DynamicCast ();
    }

    cout << "EXIT  : someFunc" << endl;
}